package database;

import java.util.*;

public class test {
	public static void main(String[] args) {

		String fileName = "test";
		Map<String, Integer> descriptor = new HashMap<String, Integer>();
		Map<String, String> record1 = new HashMap<String, String>();
		Map<String, String> record2 = new HashMap<String, String>();
		Map<String, String> record3 = new HashMap<String, String>();
		Map<String, String> record4 = new HashMap<String, String>();
		
		//init
		descriptor.put("name", 8);
		descriptor.put("age", 3);
		descriptor.put("address", 30);
		//legal record
		record1.put("name", "Tom");
		record1.put("age", "23");
		record1.put("address", "sweet home");
		record4.put("name", "Ben");
		record4.put("age", "25");
		record4.put("address", "eaglewood");
		
		//illegal record
		record2.put("name", "Lily");
		record2.put("age", "20");
		record2.put("addr", "maple"); //not include in schema
		
		record3.put("name", "Rose");
		record3.put("age", "20"); //exceed length
		record3.put("address", "ligoes");
		
		//test
		//DataManager.print(record1);
		
		DataFile df = DataManager.createFile(fileName, descriptor);
		//df.viewFile();
		
		int id1 = df.insertRecord(record1);	
		df.insertRecord(record4);
		

		//df.insertRecord(record2);

//		df.insertRecord(record2);

		
		
		/*df.viewFile();
		DataManager.print(df.getRecord(id1));
		*/
		/*df.dumpFile();
		DataFile df1=DataManager.restoreFile("test");
		df1.viewFile();*/
	//	DataManager.exit();
		for (Iterator<Integer> it =df.iterator(); it.hasNext(); ) {
		
		 int s = it.next();  // No downcasting required.
		    System.out.println(s);
		  /*  it.remove();
		   System.out.println("removed");
		    System.out.println(s);*/
		}
		/*Iterator<Integer> it1 =df.iterator();
		while(it1. hasNext()){    
		if ((Integer)it1.next()==0)  
		{it1.remove();}
		            //System.out.println(it1.next());    
		
		        }*/
		df.insertRecord(record3);
		for (Iterator<Integer> it =df.iterator(); it.hasNext(); ) {
			// System.out.println("After removed");
			 int s = it.next();  // No downcasting required.
			    System.out.println(s);
			   // it.remove();
			  // System.out.println("removed");
			  //  System.out.println(s);
			}
		for (Iterator<Integer> it =df.iterator(); it.hasNext(); ) {
			 //System.out.println("After removed");
			 int s = it.next();  // No downcasting required.
			  if(s==1){System.out.println(s+"is removed");
			  it.remove();}
			 //System.out.println(s);
			   // it.remove();
			  // System.out.println("removed");
			  //  System.out.println(s);
			}
		for (Iterator<Integer> it =df.iterator(); it.hasNext(); ) {
			 System.out.println("After removed");
			 int s = it.next();  // No downcasting required.
			    System.out.println(s);
			   // it.remove();
			  // System.out.println("removed");
			  //  System.out.println(s);
			}
		
	}
}